package com.my.screenadapter.mdir;


import android.text.TextUtils;
import android.util.Log;

import org.apache.log4j.Logger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by fubh on 2016/12/8.
 */

public class FileUtils {
private static Logger logger = Logger.getLogger(FileUtils.class);

    public static boolean  delFileIgnore(String dir,String contains,String... ignorePath){
        boolean flag = false;
        File file = new File(dir);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (dir.endsWith(File.separator)) {
                temp = new File(dir + tempList[i]);
            } else {
                temp = new File(dir + File.separator + tempList[i]);
            }
            flag = true;
            if (!TextUtils.isEmpty(tempList[i])
                    &&tempList[i].contains(contains)
                    &&temp.isFile()) {
                boolean isIgnore = false;
                for (String anIgnorePath : ignorePath) {
                    if (tempList[i].equals(anIgnorePath)) {
                        isIgnore = true;
                        break ;
                    }
                }

                //不忽略
                if (!isIgnore){
                    flag = temp.delete();
                    if (!flag){
                        logger.error("del fail path =="+temp.getAbsolutePath());
                    }
                }

            }

        }
        return flag;

    }

    public FileUtils(String path){
       // absolutePath= Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
        absolutePath=path;
    }
    private String absolutePath;
    public String getPath(){
        return absolutePath;
    }

    public File CreateSdFile(String fileName)throws IOException{
        File file=new File(absolutePath+File.separator+fileName);
        file.createNewFile();
        return file;
    }

    public static String ReadFile(String fileName){
        File f=new File(fileName);
        BufferedReader reader=null;
        StringBuilder sb=new StringBuilder();
        try {
            reader=new BufferedReader(new FileReader(f));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                sb.append(tempString);
                line++;
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        return sb.toString();

    }

    public static void CreateFile(String fileName,String content){
        File f=new File(fileName);
        if(f.exists()){
            f.delete();
        }
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        OutputStream outputStream=null;
        BufferedOutputStream Buff=null;
        try {
            outputStream=new FileOutputStream(f);
            Buff=new BufferedOutputStream(outputStream);
            Buff.write(content.getBytes());
            Buff.flush();
            Buff.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }

    }
    public File CreateSdFile(String fileName,String dir){
        File file=new File(absolutePath+dir+File.separator+fileName);
        try {
            file.createNewFile();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        return file;
    }

    public File CreateSdDir(String dirName){
        File test=new File(absolutePath);
        System.out.println(" can read ? "+test.canRead());
        System.out.println("can write ? "+test.canWrite());
        File file=new File(absolutePath+File.separator+dirName+File.separator);
        System.out.println("ready to CreateSdDir "+absolutePath+File.separator+dirName);
        //file.mkdir();

        System.out.println("CreateSdDir "+file.mkdirs());
        return file;
    }

    public boolean CheckFileExist(String fileName){
        File file=new File(absolutePath+File.separator+fileName);
        return file.exists();
    }
    public boolean CheckFileExist(String fileName,String dir){
        File file=new File(absolutePath+dir+File.separator+fileName);
        return file.exists();
    }

    public File Write2SdFromFile(String fileName,String path,InputStream inputStream){
        File file=null;
        OutputStream outputStream=null;
        try{
            CreateSdDir(path);
            file=CreateSdFile(path+File.separator+fileName);
            outputStream=new FileOutputStream(file);
            BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
            int size=0;
            int len=0;
            byte[] buf = new byte[1024];
            while((size=bufferedInputStream.read(buf))!=-1) {
                len += size;
                outputStream.write(buf,0,size);
            }
//            byte[] buffer=new byte[1024];
//            while(inputStream.read(buffer)>0){
//                outputStream.write(buffer);
//            }
//            outputStream.flush();
//            outputStream.close();
            bufferedInputStream.close();
            outputStream.close();
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
        return file;
    }

}
